﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.Text.RegularExpressions;

namespace JYH.Framework
{
    /// <summary>
    /// 序列化与反序列化帮助类
    /// </summary>
    public class ModelSerializer
    {
        /// <summary>
        /// 序列化输出到文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="FilePath">文件保存路径 例："I:\\ab.xml"</param>
        /// <returns></returns>
        public static bool SerializerToFile<T>(T model, string FilePath)
        {

            StreamWriter write = new StreamWriter(FilePath);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                serializer.Serialize(write, model);
                write.Close();


                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 序列化并返回字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="xmlPrefix">xml前缀</param>
        /// <param name="xmlNamespace">xml命名空间</param>
        /// <returns></returns>
        public static string SerializerToString<T>(T model, string xmlPrefix, string xmlNamespace)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringWriter write = new StringWriter();

            try
            {
                XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
                spaces.Add(xmlPrefix, xmlNamespace);
                serializer.Serialize(write, model, spaces);
                string Val = write.ToString().Replace("\r\n", "");
                Val = Val.Replace("utf-16", "UTF-8");
                //屏蔽节点
                Val = new Regex("<IgnoreName>").Replace(Val, "");
                Val = new Regex("</IgnoreName>").Replace(Val, "");
                Val = new Regex("<IgnoreName />").Replace(Val, "");
                return Val;
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 序列化并返回字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="emptyToPair">空串序列化为成对标签</param>
        /// <param name="xmlPrefix">xml前缀</param>
        /// <param name="xmlNamespace">xml命名空间</param>
        /// <returns></returns>
        public static string SerializerToString<T>(T model, bool emptyToPair, string xmlPrefix, string xmlNamespace)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringWriter write = new StringWriter();

            try
            {
                XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
                spaces.Add(xmlPrefix, xmlNamespace);
                serializer.Serialize(write, model, spaces);
                string Val = write.ToString().Replace("\r\n", "");
                Val = Val.Replace("utf-16", "UTF-8");
                //屏蔽节点
                Val = new Regex("<IgnoreName>").Replace(Val, "");
                Val = new Regex("</IgnoreName>").Replace(Val, "");
                Val = new Regex("<IgnoreName />").Replace(Val, "");

                if (emptyToPair)
                {
                    Regex reg = new Regex("<([a-zA-Z]*) />");

                    var gf = reg.Matches(Val);
                    foreach (Match item in gf)
                    {
                        if (item.Groups.Count > 1)
                        {
                            string elementName = item.Groups[1].Value;
                            string sourceStr = string.Format("<{0} />", elementName);
                            string targetStr = string.Format("<{0}></{1}>", elementName, elementName);
                            Val = Val.Replace(sourceStr, targetStr);
                        }
                    }
                }

                return Val;
            }
            catch (Exception)
            {
                return "";
            }
        }


        /// <summary>
        /// 将指定目录的xml反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T DeserializerWithFilePath<T>(T model, string path)
        {
            StreamReader reader = new StreamReader(path);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                object obj = serializer.Deserialize(reader);
                reader.Close();
                model = (T)obj;
                return model;
            }
            catch (Exception)
            {
                return model;
            }
        }

        /// <summary>
        /// 将xml字符串反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public static T DeserializerWithXmlString<T>(T model, string xmlString)
        {
            xmlString = xmlString.Replace("\n", "");
            xmlString = xmlString.Trim();
            xmlString = xmlString.Replace(@"\", "");
            StringReader reader = new StringReader(xmlString);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                object obj = serializer.Deserialize(reader);
                reader.Close();
                model = (T)obj;
                return model;
            }
            catch (Exception)
            {
                return model;
            }

        }
    }
}
